// The exported data interface for the tree-view inputs
export interface SnippetsDataModel {
  id: number;
  name: string;
  children?: SnippetsDataModel[];
  snippets?: Array<number>;
}

export class TreeData {
  id: number;
  name: string;
  expand: boolean;
  children: TreeData[];

  constructor(snippetsData: SnippetsDataModel) {
    this.id = snippetsData.id;
    this.name = snippetsData.name;
    this.expand = true;
    if (snippetsData.children && snippetsData.children.length) {
      this.children = [];
      for (let ch of snippetsData.children) {
        this.children.push(new TreeData(ch));
      }
    }
  }

  hasChildren() {
    return this.children && this.children.length > 0;
  }

  findNodeById(id: number): TreeData {
    if (id === this.id) {
      return this;
    } else if (this.hasChildren()) {
      for (let ch of this.children) {
        let ret = ch.findNodeById(id);
        if (ret) {
          return ret;
        }
      }
    }
    return null;
  }
}

